package com.mindskip.xzs.controller.admin;


import com.mindskip.xzs.base.*;
import com.mindskip.xzs.domain.ExamPaper;
import com.mindskip.xzs.service.WJService;
import com.mindskip.xzs.domain.WJ;
import com.mindskip.xzs.viewmodel.admin.exam.ExamPaperEditRequestVM;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

@RestController("AdminWJController")
@RequestMapping(value = "/api/admin/wj")
public class WJController extends BaseApiController {

    private final WJService wjService;

    @Autowired
    public WJController(WJService wjService) {
        this.wjService = wjService;
    }


    //分页查询
    @RequestMapping(value = "/findPage", method = RequestMethod.POST)
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean) {
        return wjService.findPage(queryPageBean);
    }

    //添加
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public Result add(@RequestBody WJ wj) {
        try{
            wjService.insertWj(wj);
            return new Result(true,MessageConstant.ADD_WJ_SUCCESS);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false, MessageConstant.ADD_WJ_FAIL);
        }
    }

    //删除
    @RequestMapping(value = "/delete/{id}", method = RequestMethod.POST)
    public Result delete(@PathVariable Integer id) {

        try{
            wjService.deleteByTwj(id);
            return new Result(true,MessageConstant.DELETE_WJ_SUCCESS);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false, MessageConstant.DELETE_WJ_FAIL);
        }
    }

    //编辑
    @RequestMapping(value = "/edit", method = RequestMethod.POST)
    public Result edit(@RequestBody WJ wj) {

        try{
            wjService.updateByTwj(wj);
            return new Result(true,MessageConstant.EDIT_WJ_SUCCESS);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false, MessageConstant.EDIT_WJ_FAIL);
        }

//        ExamPaper examPaper = examPaperService.savePaperFromVM(model, getCurrentUser());
//        ExamPaperEditRequestVM newVM = examPaperService.examPaperToVM(examPaper.getId());
//        return RestResponse.ok(newVM);
    }
}
